package com.zzrc.grape.controller;

import com.zzrc.grape.util.BasePageData;
import com.zzrc.grape.util.UserUtils;
import com.zzrc.grape.util.WebResponseCode;
import com.zzrc.grape.model.Branch;
import com.zzrc.grape.model.User;
import com.zzrc.grape.service.BranchService;
import com.zzrc.grape.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 部门操作Controller
 * @author Maochao-zhu
 *
 */
@Controller
@RequestMapping("/branch")
public class BranchController {
	protected Logger logger = LoggerFactory.getLogger(getClass());
	
	@Resource
	BranchService branchService;
	@Resource
	UserService userService;
	
	/**
	 * 去部门首页
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("goIndex")
	 public ModelAndView goIndex(HttpServletRequest request,
                                 HttpServletResponse response){
		ModelAndView modelView = new ModelAndView();
        modelView.setViewName("views/branch/index");
     return modelView; 
	}
	
	
	/**
	 * 去部门添加界面
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("goAdd")
	 public ModelAndView goAdd(HttpServletRequest request,
                               HttpServletResponse response){
		ModelAndView modelView = new ModelAndView();
		modelView.addObject("keyList",branchService.getKeyList());
        modelView.setViewName("views/branch/add");
        return modelView; 
	}
	
	/**
	 * 去编辑页面
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("goEdit")
	public ModelAndView goEdit(Integer id, HttpServletRequest request,
                               HttpServletResponse response){
		ModelAndView modelView = new ModelAndView();
		modelView.addObject("zx_data", branchService.getBranchById(id));
		modelView.addObject("keyList",branchService.getKeyList());
		modelView.setViewName("views/branch/edit");
		return modelView; 
	}
	
	/**
	 * 获取数据列表信息
	 * @param branch
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("getList")
	@ResponseBody
	 public BasePageData getList(Branch branch, HttpServletRequest request,
								 HttpServletResponse response){
		BasePageData data = new BasePageData();
		//过滤掉 ERP同步过来的部门
		branch.setNopid(4); // 列表显示过滤id= 4 和pid =4 的 部门
		List<Branch> list = branchService.getBranchList(branch);
		Integer count = branchService.getBranchListCount(branch);
		data.setCode(WebResponseCode.SUCCESS);
		data.setMsg("操作成功!");
		data.setCount(count);
		data.setData(list);
		return data;
	}





	/**
	 * 跟据id获取部门
	 * @param id
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("get")
	@ResponseBody
	public BasePageData get(Integer id,HttpServletRequest request,
			HttpServletResponse response){
		BasePageData data = new BasePageData();
		Branch branch = branchService.getBranchById(id);
		data.setData(branch);
		data.setCode(WebResponseCode.SUCCESS);
		data.setMsg("操作成功!");
		return data;
	}

}
